package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.FindTopResult;
import com.lanou.dllo.findfood.util.NewsClickListener;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class LifeRecylerAdpter extends RecyclerView.Adapter<LifeRecylerAdpter.RecylerAdpter> {
    List<FindTopResult> resultList ;
    private NewsClickListener clickListener;

    public  void  setItemLitemListener(NewsClickListener litemListener){
        clickListener = litemListener;
    }

    public  void   addData(List<FindTopResult> result){
        resultList = result;
        notifyDataSetChanged();
    }
    @Override
    public RecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyler_social_item,null);

        return new RecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(RecylerAdpter holder, int position) {
        FindTopResult result = resultList.get(position);
        if (result != null){
            holder.socialtitle.setText(result.getTitle());

            holder.socialNew.setText(result.getDescription());
            String url = result.getImg();
            String imgUrl = "http://tnfs.tngou.net/image/"+url;
            getBitmap(imgUrl,holder.socialImage);
        }

        holder.position = position;
    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }
    @Override
    public int getItemCount() {
        return resultList != null && resultList.size() > 0 ? resultList.size() :0;
    }

    class RecylerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView socialImage;
        private TextView socialtitle,socialtime,socialNew;
        private  int position;
        private LinearLayout linearLayout;

        public RecylerAdpter(View itemView) {
            super(itemView);
            socialImage = (NetworkImageView) itemView.findViewById(R.id.socialImage);
            socialNew = (TextView) itemView.findViewById(R.id.socialNew);
            //  socialtime = (TextView) itemView.findViewById(R.id.socialtime);
            socialtitle = (TextView) itemView.findViewById(R.id.socialtitle);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            FindTopResult result = resultList.get(position);
            if (clickListener != null){
                clickListener.itemClickListener(result);
            }

        }
    }
}